package org.liukai.ems.controller;

import org.liukai.ems.entity.po.Emp;
import org.liukai.ems.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @create EmpController.java 10:53
 * 
 * @author liukai
 * @date 2015/12/11
 */
@Controller
@RequestMapping("/emp")
public class EmpController extends BaseController{
    Logger logger = LoggerFactory.getLogger(EmpController.class);

    //注入
    @Resource
    private EmpService empService;
    
    //查所有
    @RequestMapping("/empList")
    public ModelAndView queryEmpList(){
        ModelAndView model = new ModelAndView("emp/empList");
        List<Emp> list =  empService.queryEmpList();
        model.addObject("empList", list);
        return model;
    }
    
    //跳转添加页面
    @RequestMapping("/addEmpView")
    public ModelAndView addEmpView(){
        ModelAndView model = new ModelAndView("emp/addEmp");
        return model;
    }
    
    //添加emp
    @RequestMapping("/addEmp")
    public ModelAndView addEmp(Emp emp){
        empService.addEmp(emp);
        return new ModelAndView("redirect:/emp/empList");
    }

    //跳转修改页面
    @RequestMapping("/updateEmpView")
    public ModelAndView updateEmpView(String empId){
        ModelAndView model = new ModelAndView("emp/updateEmp");
        Emp emp = empService.queryEmpByEmpId(empId);
        model.addObject("emp", emp);
        return model;
    }

    //修改
    @RequestMapping("/updateEmp")
    public ModelAndView updateEmp(Emp emp){
        empService.updateEmp(emp);
        return new ModelAndView("redirect:/emp/empList");
    }

    //删除
    @RequestMapping("/deleteEmp")
    public ModelAndView deleteEmp(String empId){
        empService.deleteEmp(empId);
        return new ModelAndView("redirect:/emp/empList");
    }

    public EmpController(){
        System.out.println("实例化controller");
    }

}
